上一篇的Bookinfo這個服務中,我們有使用samples/bookinfo/networking/bookinfo-gateway.yaml進行部署。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*" 
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080
hosts可以使用多組host name,主要是針對要導入VirtualService的host,需要設定在這邊,像下圖的例子,當host name為下面例子時才會導流進來。
VirtualService可以為IP或是DNS
hosts:
  - "www.imtest.com" 
  - "www.imtest.net"
這邊使用了http是這區域定義了路由規則,match表示要符合規則的才會導入到對應的服務。
exact: /productpage : 絕對路徑prefix: /static : 前綴static的路徑regex : 使用正則式uri,外還可以使用headers來判斷。route : 當符合match的路由條件時,VirtualService就會把流量導到host的服務http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    - headers: ## header裡面有end-user=jason
        end-user:
          exact: jason
    route:
    - destination:
        host: productpage
        port:
          number: 9080      
也可以寫成一個VirtualService 多組路由規則,下面的路由規則設定了從bookinfo-gateway進來流量,透過不同的path導至不同的服務的需求。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*" 
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage1    
    route:
    - destination:
        host: productpage
        port:
          number: 9080
    name: "route1"
  - match:    
    - uri:
        prefix: /static   
    route:
    - destination:
        host: productpage
        port:
          number: 9080
    name: "route2"
  - match:    
    - uri:
        prefix: /test2   
    route:
    - destination:
        host: productpage2
        port:
          number: 9080
    name: "route3"
以上是上面case中會用到的用法,如果需要更多的用法可以參考官網的文件喔
官方virtual-service